package com.nnfe.fyd.feature.main;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.KeyEvent;

import com.nnfe.fyd.AppConfig;
import com.nnfe.fyd.R;
import com.nnfe.fyd.ShareKey;
import com.nnfe.fyd.base.BaseActivity;
import com.nnfe.fyd.feature.inquiry.MyInquiryFragment_list;
import com.nnfe.fyd.feature.login.LoginActivity;
import com.nnfe.fyd.utils.SharePrefUtil;
import com.nnfe.fyd.view.TabBar;


public class MainActivity extends BaseActivity implements TabBar.TabListener {


    private static final String CHECKED_INDEX = "INDEX";

    public static void launch(Activity context) {
        Intent intent = new Intent();
        intent.setClass(context, MainActivity.class);
        context.startActivity(intent);
        context.overridePendingTransition(R.anim.open_enter,R.anim.open_exit);
    }


    /**
     * @param context
     * @param tabNum  切到第几页， 0 - 首页； 1 - 订单页； 2 - 查查页； 3 - 我的页。
     */
    public static void launch(Context context, int tabNum) {
        Intent intent = new Intent();
        intent.putExtra(CHECKED_INDEX, tabNum);
        intent.setClass(context, MainActivity.class);
        context.startActivity(intent);
    }
    private TabBar mTabBar;
    private int index = 0;

    @Override
    public Fragment getContentFragment() {
        return null;
    }

    @Override
    public int getContentView() {
        return R.layout.activity_main;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        mTabBar = (TabBar) findViewById(R.id.fragment_sailfishbar);
        mTabBar.setTabListener(this);

        if (savedInstanceState == null) {
            processIntent();
        } else {
            int checkedIndex = savedInstanceState.getInt(CHECKED_INDEX);
            mTabBar.changeTab(checkedIndex);
        }
    }

    private void processIntent() {
        index = getIntent().getIntExtra(CHECKED_INDEX, 0);
        if (mTabBar != null) {
            mTabBar.changeTab(index);
        }
    }


    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        //must store the new intent unless getIntent() will return the old one
        setIntent(intent);
        processIntent();
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt(CHECKED_INDEX, index);
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        savedInstanceState.putInt(CHECKED_INDEX, index);
        index = savedInstanceState.getInt(CHECKED_INDEX);
    }

    private Fragment tabOne = new MainFragmentTabOne();
    private Fragment tabTwo;
    private Fragment tabThree;
    private Fragment order;

    @Override
    public void showFragment(String fragmentName) {
        Fragment fragment = getSupportFragmentManager().findFragmentByTag(fragmentName);

        if (fragment == null) {
            if (fragmentName.equals(AppConfig.FRAGMENT_TAB + 0)) {
                if (tabOne == null) {
                    tabOne = new MainFragmentTabOne();
                }
                fragment = tabOne;
            } else if (fragmentName.equals(AppConfig.FRAGMENT_TAB + 2)) {
                if (tabTwo == null) {
                    tabTwo = new MainFragmentTabTwo();
                }
                fragment = tabTwo;
            } else if (fragmentName.equals(AppConfig.FRAGMENT_TAB + 3)) {
                if (tabThree == null) {
                    tabThree = new MainFragmentTabThree();
                }
                fragment = tabThree;
            }else if (fragmentName.equals(AppConfig.FRAGMENT_TAB + 1)) {
                if (!SharePrefUtil.getBoolean(mContext, ShareKey.isLogin,false)){
                    LoginActivity.lauch(mContext, LoginActivity.TYPE_LOGIN);
                    mTabBar.changeTab(0);
                    return;
                }else {
                    if (order == null) {
                        order = new MyInquiryFragment_list();
//                    order = new MyInquiryFragment();
                    }
                    fragment = order;
                }
            }
        }if (fragment!=null && fragmentName.equals(AppConfig.FRAGMENT_TAB + 1)){
            // 已经生成fragment  检测是否登录
            if (!SharePrefUtil.getBoolean(mContext, ShareKey.isLogin,false)){
                LoginActivity.lauch(mContext, LoginActivity.TYPE_LOGIN);
                mTabBar.changeTab(0);
                return;
            }

        }

        if (!fragment.isAdded()) {
            add(R.id.tab_content, fragment, fragmentName);
        }

        show(fragment);

    }

    @Override
    public void hideFragment(String fragmentName) {
        Fragment fragment = getSupportFragmentManager().findFragmentByTag(fragmentName);
        if (fragment != null) {
            hide(fragment);
        }
    }

    @Override
    public void refreshView(String fragmentName) {

    }


    private long mExitTime;

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if ((System.currentTimeMillis() - mExitTime) > 2000) {
                mExitTime = System.currentTimeMillis();
                //ToastUtil.showLongToast(mContext, "再按一次退出程序");
            } else {
                this.onDestroy();   // 释放资源
                finish();           // 移出栈
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }


}
